﻿using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Jsl.FxCop
{
    [TestClass]
    public class When_checking_if_a_method_is_empty
    {
        [TestMethod]
        public void Empty_debug_method_is_considered_empty()
        {
            var compileHelper = new CompileHelper()
                .AddMethod(@"
public void Method()
{
}");
            new CodeTestHelper(compileHelper)
                .Test(
                    debugMode: true,
                    action: method => AssertEx.That(() => InstructionHelper.IsEmptyMethod(method)));
        }

        [TestMethod]
        public void Empty_release_method_is_considered_empty()
        {
            var compileHelper = new CompileHelper()
                .AddMethod(@"
public void Method()
{
}");
            new CodeTestHelper(compileHelper)
                .Test(
                    debugMode: false,
                    action: method => AssertEx.That(() => InstructionHelper.IsEmptyMethod(method)));
        }

        [TestMethod]
        public void Debug_method_with_only_return_is_NOT_considered_empty()
        {
            // When compiling in Debug mode, the compiler sticks in a Branch to the Return statement.

            var compileHelper = new CompileHelper()
                .AddMethod(@"
public void Method()
{
    return;
}");
            new CodeTestHelper(compileHelper)
                .Test(
                    debugMode: true,
                    action: method => AssertEx.That(() => !InstructionHelper.IsEmptyMethod(method)));
        }

        [TestMethod]
        public void Release_method_with_only_return_is_considered_empty()
        {
            // When compiling in Release mode, the return statement basically gets optimized out.

            var compileHelper = new CompileHelper()
                .AddMethod(@"
public void Method()
{
    return;
}");
            new CodeTestHelper(compileHelper)
                .Test(
                    debugMode: false,
                    action: method => AssertEx.That(() => InstructionHelper.IsEmptyMethod(method)));
        }
    }
}